home *** CD-ROM | disk | FTP | other *** search
/ EnigmA Amiga Run 1997 February / EnigmA AMIGA RUN 15 (1997)(G.R. Edizioni)(IT)[!][issue 1997-02][PLANET CD V].iso / progs / sviluppo / pike-0.4.0 / lib / include / array.pre.pike next >
Text File  |  1997-01-10  |  3KB  |  175 lines

  1. #define error(X) throw( ({ (X), backtrace()[0..sizeof(backtrace())-2] }) )
  2.  
  3. mixed map(mixed arr, mixed fun, mixed ... args)
  4. {
  5.   int e;
  6.   mixed *ret;
  7.  
  8.   if(mappingp(arr))
  9.     return mkmapping(indices(arr),map(values(arr),fun,@args));
  10.  
  11.   if(intp(fun))
  12.     return arr(@args);
  13.   
  14.   if(stringp(fun))
  15.     return column(arr, fun)(@args);
  16.  
  17.   if(functionp(fun))
  18.   {
  19.     ret=allocate(sizeof(arr));
  20.     for(e=0;e<sizeof(arr);e++)
  21.       ret[e]=fun(arr[e],@args);
  22.     return ret;
  23.   }
  24.  
  25.   error("Bad argument 2 to map_array().\n");
  26. }
  27.  
  28. mixed filter(mixed arr, mixed fun, mixed ... args)
  29. {
  30.   int e;
  31.   mixed *ret;
  32.  
  33.   if(mappingp(arr))
  34.   {
  35.     mixed *i, *v, r;
  36.     i=indices(arr);
  37.     ret=map(v=values(arr),fun,@args);
  38.     r=([]);
  39.     for(e=0;e<sizeof(ret);e++) if(ret[e]) r[i[e]]=v[e];
  40.  
  41.     return ret;
  42.   }else{
  43.     int d;
  44.     ret=map(arr,fun,@args);
  45.     for(e=0;e<sizeof(arr);e++) if(ret[e]) ret[d++]=arr[e];
  46.     
  47.     return ret[..d-1];
  48.   }
  49. }
  50.  
  51.  
  52. int search_array(mixed *arr, mixed fun, mixed ... args)
  53. {
  54.   int e;
  55.  
  56.   if(stringp(fun))
  57.   {
  58.     for(e=0;e<sizeof(arr);e++)
  59.       if(arr[e][fun](@args))
  60.     return e;
  61.     return -1;
  62.   }
  63.   else if(functionp(fun))
  64.   {
  65.     for(e=0;e<sizeof(arr);e++)
  66.       if(fun(arr[e],@args))
  67.     return e;
  68.     return -1;
  69.   }
  70.   else if(intp(fun))
  71.   {
  72.     for(e=0;e<sizeof(arr);e++)
  73.       if(arr[e](@args))
  74.     return e;
  75.     return -1;
  76.   }
  77.   else
  78.   {
  79.     error("Bad argument 2 to filter().\n");
  80.   }
  81. }
  82.  
  83. mixed *sum_arrays(function foo, mixed * ... args)
  84. {
  85.   mixed *ret;
  86.   int e,d;
  87.   ret=allocate(sizeof(args[0]));
  88.   for(e=0;e<sizeof(args[0]);e++)
  89.     ret[e]=foo(@ column(args, e));
  90.   return ret;
  91. }
  92.  
  93. varargs mixed *sort_array(array foo,function cmp, mixed ... args)
  94. {
  95.   array bar,tmp;
  96.   int len,start;
  97.   int length;
  98.   int foop, fooend, barp, barend;
  99.  
  100.   if(!cmp || cmp==`>)
  101.   {
  102.     foo+=({});
  103.     sort(foo);
  104.     return foo;
  105.   }
  106.  
  107.   if(cmp == `<)
  108.   {
  109.     foo+=({});
  110.     sort(foo);
  111.     return reverse(foo);
  112.   }
  113.  
  114.   length=sizeof(foo);
  115.  
  116.   foo+=({});
  117.   bar=allocate(length);
  118.  
  119.   for(len=1;len<length;len*=2)
  120.   {
  121.     start=0;
  122.     while(start+len < length)
  123.     {
  124.       foop=start;
  125.       barp=start+len;
  126.       fooend=barp;
  127.       barend=barp+len;
  128.       if(barend > length) barend=length;
  129.       
  130.       while(1)
  131.       {
  132.     if(cmp(foo[foop],foo[barp],@args) <= 0)
  133.     {
  134.       bar[start++]=foo[foop++];
  135.       if(foop == fooend)
  136.       {
  137.         while(barp < barend) bar[start++]=foo[barp++];
  138.         break;
  139.       }
  140.     }else{
  141.       bar[start++]=foo[barp++];
  142.       if(barp == barend)
  143.       {
  144.         while(foop < fooend) bar[start++]=foo[foop++];
  145.         break;
  146.       }
  147.     }
  148.       }
  149.     }
  150.     while(start < length) bar[start]=foo[start++];
  151.  
  152.     tmp=foo;
  153.     foo=bar;
  154.     bar=tmp;
  155.   }
  156.  
  157.   return foo;
  158. }
  159.  
  160. array uniq(array a)
  161. {
  162.   return indices(mkmapping(a,a));
  163. }
  164.  
  165.  
  166. void create()
  167. {
  168.   add_constant("filter",filter);
  169.   add_constant("map",map);
  170.   add_constant("sum_arrays",sum_arrays);
  171.   add_constant("search_array",search_array);
  172.   add_constant("sort_array",sort_array);
  173.   add_constant("uniq",uniq);
  174. }
  175.